package DBCP;

import org.apache.commons.dbcp.BasicDataSource;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class MyDBCP{
    private static BasicDataSource basicDataSource = new BasicDataSource();
    static {
         basicDataSource.setUrl(DB.URL);
         basicDataSource.setUsername(DB.USERNAME);
         basicDataSource.setPassword(DB.PASSWORD);
         basicDataSource.setDriverClassName("com.mysql.jdbc.Driver");
    }
    public static Connection getConnection(){
        try {
          return  basicDataSource.getConnection();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

    }
    public static void close(ResultSet resultSet, PreparedStatement preparedStatement,Connection connection){
        if (resultSet != null){
            try {
                resultSet.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
        if (preparedStatement != null){
            try {
                preparedStatement.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
        if (connection != null){
            try {
                connection.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
    }
}
